﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data.Sql;
using System.Data;
using System.Data.SqlClient;
using GriffinWebService.Entity;
using GriffinWebService.BE;

namespace GriffinWebService.DALC
{
    public class EvaluationDALC
    {
        #region Fields
        private SqlHelper sqlHelper = new SqlHelper();
        #endregion

        #region Fields

        public List<Evaluation> getCateEvaluation(int cateID)
        {
            string statement = string.Empty;
            IDataReader dataReader = null;
            List<Evaluation> resultList = new List<Evaluation>();

            #region
            try
            {

                statement = "dbo.spu_evaluation_find_by_userid_cateid";
                sqlHelper.AddParameterToSQLCommand("@cateid", SqlDbType.NVarChar, cateID);
                dataReader = sqlHelper.GetReaderByCmd(statement);
                while (dataReader.Read())
                {
                    Evaluation result = new Evaluation();
                    //int userIDIndex = dataReader.GetOrdinal("UserID");
                    //if (!dataReader.IsDBNull(userIDIndex))
                    //{
                    //    result.UserID = dataReader.GetInt32(userIDIndex);
                    //}

                    int siteIDIndex = dataReader.GetOrdinal("SiteID");
                    if (!dataReader.IsDBNull(siteIDIndex))
                    {
                        result.SiteID = dataReader.GetInt32(siteIDIndex);
                    }

                    int evaluationNumberIndex = dataReader.GetOrdinal("EvaluationNumber");
                    if (!dataReader.IsDBNull(evaluationNumberIndex))
                    {
                        result.EvaluationNumber = dataReader.GetDecimal(evaluationNumberIndex);
                    }

                    resultList.Add(result);
                }
            }
            finally
            {
                sqlHelper.Dispose();
            }
            #endregion


            return resultList;
        }

        public decimal getSiteEvaluation(int userID, int siteID)
        {
            string statement = string.Empty;
            IDataReader dataReader = null;
            decimal result = 0;

            #region
            try
            {

                statement = "dbo.spu_evaluation_find_by_userid_siteid";
                sqlHelper.AddParameterToSQLCommand("@userid", SqlDbType.Int, userID);
                sqlHelper.AddParameterToSQLCommand("@siteid", SqlDbType.Int, siteID);
                dataReader = sqlHelper.GetReaderByCmd(statement);
                if (dataReader.Read())
                {
                    int resultIndex = dataReader.GetOrdinal("EvaluationNumber");
                    if (!dataReader.IsDBNull(resultIndex))
                    {
                        result = dataReader.GetDecimal(resultIndex);
                    }

                }
            }
            finally
            {
                sqlHelper.Dispose();
            }
            #endregion


            return result;
        }

        public int setEvaluation(int userID, int siteID, int cateid, decimal evaluationNumber)
        {
            string statement = string.Empty;
            IDataReader dataReader = null;
            int result = 0;

            #region
            try
            {

                statement = "dbo.spu_evaluation_create";
                sqlHelper.AddParameterToSQLCommand("@userid", SqlDbType.Int, userID);
                sqlHelper.AddParameterToSQLCommand("@siteid", SqlDbType.Int, siteID);
                sqlHelper.AddParameterToSQLCommand("@cateid", SqlDbType.Int, cateid);
                sqlHelper.AddParameterToSQLCommand("@evaluationnumber", SqlDbType.Decimal, evaluationNumber);

                int res = sqlHelper.GetExecuteScalarByCommand(statement);
                result = res ;

            }
            finally
            {
                sqlHelper.Dispose();
            }
            #endregion


            return result;
        }

        #endregion
    }
}
